﻿using ConfExpert.Forms.Base;
using DevExpress.XtraGrid.Views.Base;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Cikis
{
    public partial class CikisEmriSatisByIrsaliye : XForm
    {
        public DataTable gV2DataTable = null;
        public CikisEmriSatisByIrsaliye()
        {
            InitializeComponent();
        }

        private void CikisEmriSatisByIrsaliye_Load(object sender, EventArgs e)
        {
            if (!ytk(238))
                btnKaydet.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            int who = isKD ? -1 : WhoAmI;
            gridView1.Init("GetCikisEmriSatisByIrsaliye", who);
            gridView1.AddDetailGrid(gridView2, "IrsaliyeNumber,IrsaliyeTarihi,SatilanYer,SatisTarihi,OlusturanId");

            gridView2.AddColumn("ToplamFiyat", typeof(int), "Qty*Fiyat");
            gridView1.AddColumn("Eksik", typeof(int), "SUM(Child({FK}).Adet)", 0);
            gridView1.SetToolTip("Eksik:İrsaliyedeki Fiyat Girilmemiş Artikellerin Sayısı");
            gridView2.FormatAsNumber(1, "Qty");
            gridView2.FormatAsNumber(2, "Fiyat");
            gridView2.FormatAsNumber(1, "ToplamFiyat");
            gridView1.YanYanaGetirSoluna("Eksik", "OlusturanId");
            gridView2.AddSummary("Qty", "ToplamFiyat");
            gridView1.AddSummary();
            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly("Fiyat", "CurrencyId");
            gridView2.MakeColumnInvisible("Adet");
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gridView1.MoveLast();
            gridView1.MakeRowVisible(gridView1.FocusedRowHandle);
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            gV2DataTable = gridView2.DataTable();
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
        }

        private DataTable gV2DegisiklikVarMi()
        {
            DataTable retTable = new DataTable();
            retTable.Columns.Add(new DataColumn("CikisEmriId", typeof(int)));
            retTable.Columns.Add(new DataColumn("ProductId", typeof(int)));
            retTable.Columns.Add(new DataColumn("Fiyat", typeof(double)));
            retTable.Columns.Add(new DataColumn("CurrencyId", typeof(int)));

            DataTable tableGv2 = gridView2.DataTable();

            for (int i = 0; i < gV2DataTable.Rows.Count; i++)
            {
                for (int j = 0; j < tableGv2.Rows.Count; ++j)
                {
                    if (tableGv2.Rows[j]["CikisEmriId"].ToInt() == gV2DataTable.Rows[i]["CikisEmriId"].ToInt()
                        && tableGv2.Rows[j]["ProductId"].ToInt() == gV2DataTable.Rows[i]["ProductId"].ToInt())
                    {
                        if (tableGv2.Rows[j]["Fiyat"].ToDecimal() != gV2DataTable.Rows[i]["Fiyat"].ToDecimal()
                            || tableGv2.Rows[j]["CurrencyId"].ToInt() != gV2DataTable.Rows[i]["CurrencyId"].ToInt())
                        {
                            DataRow row = retTable.NewRow();
                            row[0] = tableGv2.Rows[j]["CikisEmriId"].ToInt();
                            row[1] = tableGv2.Rows[j]["ProductId"].ToInt();
                            row[2] = tableGv2.Rows[j]["Fiyat"].ToDecimal();
                            row[3] = tableGv2.Rows[j]["CurrencyId"].ToInt();
                            retTable.Rows.Add(row);
                        }
                        break;
                    }
                }
            }

            return retTable;
        }

        private void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView2.EndEditing();
            String xml = gridView2.SerializeChanges();
            /*
            DataTable table = gV2DegisiklikVarMi();  // Degisikligi yakalayamiyor
            if (table.Rows.Count > 0)
            {
                DataTableBastir(table);*/
                int RowHandle = gridView1.FocusedRowHandle;
                ExecuteNonQuery("UpdCikisEmriSatis2", xml);  //table.Serialize());
                int who = isKD ? -1 : WhoAmI;
                gridView1.RefreshDataSource("GetCikisEmriSatisByIrsaliye", who);
                gridView1.FocusedRowHandle = RowHandle;
            //}

        }
        
    }
}
